﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using UnityScriptEngine;

namespace EnrollmentSystem
{
    public partial class frmPaymentEntry : Form
    {
        UnityProcessor proc = new UnityProcessor();
        UnityScript[] dat = new UnityScript[0];
        Threader t = new Threader();

        DataTable items = new DataTable();
        DataTable students = new DataTable();
        DataTable payment = new DataTable();
        DataTable enrollment = new DataTable();

        string curPO = "";
        string wrnEntMsg = "";
        float gtotal = 0f;

        bool qtyErr = false, prcErr = false,
            cmtErr = false,Committing = false, stdErr = false,
            isViewing = false;

        //AutoChecker
        Timer tmr = new Timer();
      
        public frmPaymentEntry()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
            
        }

        private void InitializeForm()
        {
            proc.ProcessCompleted += new ProcessHandler(proc_ProcessCompleted);
            proc.ProcessError += new ProcessHandler(proc_ProcessError);
                
            //Start AutoCheck timer
            txtPrice.TextChanged += new EventHandler(CheckValue);
            txtName.TextChanged += new EventHandler(txtName_TextChanged);
            txtName.LostFocus += new EventHandler(txtName_LostFocus);
            txtStudentNumber.KeyPress += new KeyPressEventHandler(txtStudentNumber_KeyPress);
            txtStudentNumber.LostFocus += new EventHandler(txtStudentNumber_LostFocus);
            txtStudentNumber.TextChanged += new EventHandler(txtStudentNumber_TextChanged);
            lstEnrollmentNumber.SelectedIndexChanged += new EventHandler(lstEnrollmentNumber_SelectedIndexChanged);


            btnCommit.Click += new EventHandler(btnCommit_Click);
            btnCommit.LostFocus += new EventHandler(delegate(object sender, EventArgs e)
            {
                txtStudentNumber.Focus();
            });
            
            t.MethodCompleted += new EventHandler(t_MethodCompleted);
            btnCommit.Visible = false;
            
            InitializeItemTable();
            LoadData();
            txtStudentNumber.Focus();
            btnView.Click += new EventHandler(btnView_Click);
            btnPrint.Click +=new EventHandler(btnPrint_Click);
        }

        void btnView_Click(object sender, EventArgs e)
        {
            SearchData();
            //ConstructItems();
            //grdItems.DataSource = items;
        }

        void lstEnrollmentNumber_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRow[] dr = enrollment.Select("fEnrollmentNumber='" + lstEnrollmentNumber.Text + "'");
            lstAYsem.Items.Clear();
            for (int i = 0; i < dr.Length; i++)
            {
                lstAYsem.Items.Add(dr[i]["fSchoolYear"].ToString());
            }
            if (dr.Length > 0)
            {
                lstAYsem.SelectedIndex = 0;
            }
        }

        void txtName_LostFocus(object sender, EventArgs e)
        {
            LoadEnrollmentInfo();
        }

        void txtStudentNumber_TextChanged(object sender, EventArgs e)
        {
             DataRow[] dr = students.Select("fNumber='" + txtStudentNumber.Text + "'");
             if (dr.Length != 1)
             {
                 lblWarningEntry.Text = "Warning: Student not found in the database.";
                 stdErr = true;
             }
             else
             {
                 stdErr = false;
             }
        }

        void txtStudentNumber_LostFocus(object sender, EventArgs e)
        {
            CheckAndLoad();
            LoadEnrollmentInfo();
        }

        void txtStudentNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                CheckAndLoad();
                LoadEnrollmentInfo();
            }
        }
        
        void txtName_TextChanged(object sender, EventArgs e)
        {
            DataRow[] dr = students.Select("fName='" + txtName.Text + "'");
            if (dr.Length == 1)
            {
                txtStudentNumber.Text = dr[0]["fNumber"].ToString();
                txtCourse.Text = dr[0]["fCourse"].ToString();
                
                txtStudentNumber.ForeColor = Color.Black;
                txtName.ForeColor = Color.Black;
                txtCourse.ForeColor = Color.Black;
                
                lblWarningEntry.Text = "";
                stdErr = false;
            }
            else //if (txtSupplierName.Text.Length > 0)
            {
                if (!stdErr)
                {
                    txtStudentNumber.Text = "N/A";
                    txtCourse.Text = "N/A";

                    txtName.ForeColor = Color.Red;
                    txtStudentNumber.ForeColor = Color.Red;
                    txtCourse.ForeColor = Color.Red;
                   
                    lblWarningEntry.Text = "Warning: Student not found in the database.";
                    stdErr = true;
                }
            }
           
        }

        void btnPrint_Click(object sender, EventArgs e)
        {
            if (grdItems.Rows.Count > 0)
            {
                PrintPO();
            }
        }


        void btnCommit_Click(object sender, EventArgs e)
        {
            if (txtOR.Text != "" && txtStudentNumber.Text != "" && lstEnrollmentNumber.Text != "")
            {
                DialogResult dlg = MessageBox.Show("Please double check your entries before saving.\r\n" +
                    "Do you wish to continue saving?", "Commit", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg == System.Windows.Forms.DialogResult.No) return;

                CommitPayments();
            }
        }
        
        void CheckValue(object sender, EventArgs e)
        {
            t.MethodCall(CheckEntry);
        }
        //Process Ended
        void proc_ProcessError(string id, string desc)
        {
            t.SafeCall(this, delegate
            {
                
                MessageBox.Show(desc);
                States(true);
                switch (id)
                {
                    case "COMMIT":
                        Committing = false;
                        break;
                }
                tmr.Enabled = true;
            });
        }
        void proc_ProcessCompleted(string id, string desc)
        {
            t.SafeCall(this, delegate
            {
                switch (id)
                {
                    case "COMMIT":
                        SearchData();
                        break;
                    case "SEARCH":
                        UnityScript[] esdat;
                        proc.DBPUX_Data_Fetch(out esdat);
                        payment = esdat[0].ToDataTable();
                        items.Rows.Clear();
                        ConstructItems();

                        txtName.Text = "";
                        txtCourse.Text = "";
                        txtOR.Text = "";
                        txtPrice.Text = "";
                        txtStudentNumber.Text = "";
                        lstEnrollmentNumber.Items.Clear();
                        lstAYsem.Items.Clear();

                        txtStudentNumber.Focus();
                        break;
                    case "LOADENROLLMENTINFO":
                        UnityScript[] edat;
                        proc.DBPUX_Data_Fetch(out edat);
                        
                        enrollment = edat[0].ToDataTable();
                        lstEnrollmentNumber.Items.Clear();
                        if (edat[0].Data_Count > 0)
                        {
                            lstEnrollmentNumber.Items.AddRange(edat[0].ToList<string>(0));
                            lstEnrollmentNumber.SelectedIndex = 0;
                        }
                       

                        break;
                    case "LOAD":
                        proc.DBPUX_Data_Fetch(out dat);
                        students = dat[0].ToDataTable();

                        txtStudentNumber.AutoCompleteCustomSource.AddRange(dat[0].ToList<string>(0));
                        txtName.AutoCompleteCustomSource.AddRange(dat[0].ToList<string>(1));

                        lstPaymentFor.Items.Clear();
                        lstPaymentFor.Items.AddRange(dat[1].ToList<string>(0));
                        lstPaymentFor.SelectedIndex = 0;

                        items.Rows.Clear();
                        payment = dat[2].ToDataTable();

                        ConstructItems();
                        
                        lblWarningEntry.Text = "Loaded @ " + proc.ProcEnd() + " sec.";
                        break;
                };
                if (Committing || isViewing) return;
                tmr.Enabled = true;
                States(true);
            });
        }
        void t_MethodCompleted(object sender, EventArgs e)
        {
            t.SafeCall(this, delegate
            {
                switch(sender.ToString())
                {
                    case "CheckEntry":
                        lblWarningEntry.Text = wrnEntMsg;

                        if (prcErr)
                            txtPrice.ForeColor = Color.Red;
                        else
                            txtPrice.ForeColor = Color.Black;

                        if (!prcErr)
                            btnCommit.Visible = true;
                        else
                            btnCommit.Visible = false;

                        txtTotal.Text = gtotal.ToString("#,#0.00");
                        btnCommit.Visible = !cmtErr;
                        break;
                    case "CheckContent":
                        btnCommit.Visible = !cmtErr;
                        break;
                }
                if (Committing) return;
                tmr.Enabled = true;
            });
        }

        //Operations
        void InitializeItemTable()
        {
            items.Columns.Add("Particulars");
            items.Columns.Add("OR Number");
            items.Columns.Add("Cash");
            items.Columns.Add("Tuition Fees");
            items.Columns.Add("Registration and Misc");

            items.Columns[2].DataType = typeof(float);
            items.Columns[2].DefaultValue = 0.00f;
            items.Columns[3].DataType = typeof(float);
            items.Columns[3].DefaultValue = 0.00f;
            items.Columns[4].DataType = typeof(float);
            items.Columns[4].DefaultValue = 0.00f;

            grdItems.DataSource = items;

            grdItems.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.True;

            grdItems.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdItems.Columns[3].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            grdItems.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            grdItems.Columns[2].DefaultCellStyle.Format = "#,#0.00";
            grdItems.Columns[3].DefaultCellStyle.Format = "#,#0.00";
            grdItems.Columns[4].DefaultCellStyle.Format = "#,#0.00";

        }
        void CheckEntry()
        {
            float fprice = 0f;
            string price = txtPrice.Text;
            
            //Check Price
            if (!float.TryParse(price, out fprice))
            {
                prcErr = true;
                wrnEntMsg = "";
                wrnEntMsg += "Invalid AMOUNT value detected!\r\n";
            }
            else
            {
                prcErr = false;
                if (!qtyErr) wrnEntMsg = "";
            }
            //Compute for the total
            gtotal = 0;
            for (int i = 0; i < items.Rows.Count; i++)
            {
                gtotal += (float)items.Rows[i]["Cash"];
            }
        }
        void LoadData()
        {
            while (proc.IsRunning)
            {
                System.Threading.Thread.Sleep(1);
            }
            proc.ProcStart();
            UnityScript x = new UnityScript("LOAD", 4);
            x.Script_Add(Script.DBPUX_BEGIN);
            x.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fNumber, fName, fCourse, fStamp FROM tblStudentProfile"));
            x.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fData, fStamp FROM tblLookUp WHERE fKey = 'PAYMENT_FOR'"));
            x.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fName, fOfficialReceipt, fPaymentFor, fDate,  fPaymentAmount,fStamp FROM tblPayments " +
                    "WHERE (fDate = '{0}') ORDER BY fOfficialReceipt ASC", txtSearchDate.Value.ToString("yyyy-MM-dd")));
            /*
            x.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fName AS 'Particulars', t1.fOfficialReceipt AS 'Official Receipt', fTuition AS 'Tuition Fee', fMisc AS 'Registration and Misc Fee',  (fTuition+fMisc) AS 'Cash',fStamp FROM tblPayments t1, "+
                    "(SELECT t22.fOfficialReceipt, fPaymentAmount AS fTuition FROM tblpayments AS t22 WHERE fPaymentFor='TUITION FEE') AS t2, " +
                    " (SELECT t33.fOfficialReceipt, fPaymentAmount AS fMisc FROM tblpayments AS t33 WHERE fPaymentFor='REGISTRATION AND MISC FEE') AS t3 " +
                    "WHERE (t1.fOfficialReceipt = t2.fOfficialReceipt) AND (t1.fOfficialReceipt = t3.fOfficialReceipt) AND (fDate = '{0}') GROUP BY t1.fOfficialReceipt", txtSearchDate.Value.ToString("yyyy-MM-dd")));
            
             */
            x.Script_Add(Script.DBPUX_COMMIT);
            proc.Run(x);
        }
        void CheckAndLoad()
        {
            DataRow[] dr = students.Select("fNumber='" + txtStudentNumber.Text + "'");
            if (dr.Length == 1)
            {
                txtName.Text = dr[0]["fName"].ToString();
                txtCourse.Text = dr[0]["fCourse"].ToString();

                txtStudentNumber.ForeColor = Color.Black;
                txtName.ForeColor = Color.Black;
                txtCourse.ForeColor = Color.Black;

                lblWarningEntry.Text = "";
                stdErr = false;
            }
            else //if (txtSupplierName.Text.Length > 0)
            {
                if (!stdErr)
                {
                    txtName.Text = "N/A";
                    txtCourse.Text = "N/A";

                    txtName.ForeColor = Color.Red;
                    txtStudentNumber.ForeColor = Color.Red;
                    txtCourse.ForeColor = Color.Red;

                    lblWarningEntry.Text = "Warning: Student not found in the database.";
                    stdErr = true;
                }
            }
        }
        void LoadEnrollmentInfo()
        {
            if (stdErr == false)
            {
                UnityScript u = new UnityScript("LOADENROLLMENTINFO", 4);
                u.Script_Add(Script.DBPUX_BEGIN);
                u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fEnrollmentNumber, fStudentNumber, fSchoolYear, fStamp FROM tblEnrollment WHERE fStudentNumber = '{0}'",txtStudentNumber.Text));
                //u.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT DISTINCT fSchoolYear FROM tblEnrollment WHERE fStudentNumber = '{0}'", txtStudentNumber.Text));
                u.Script_Add(Script.DBPUX_COMMIT);
                proc.Run(u);
            }
        }
        void SearchData()
        {
            UnityScript us = new UnityScript("SEARCH", 4);
            us.Script_Add(Script.DBPUX_BEGIN);
            us.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fName, fOfficialReceipt, fPaymentFor, fDate,  fPaymentAmount,fStamp FROM tblPayments " +
                    "WHERE (fDate = '{0}') ORDER BY fOfficialReceipt ASC ", txtSearchDate.Value.ToString("yyyy-MM-dd")));
            /*
            us.Script_Add(Script.DBPUX_QUERY, string.Format("SELECT fID, fName AS 'Particulars', t1.fOfficialReceipt AS 'Official Receipt', fTuition AS 'Tuition Fee', fMisc AS 'Registration and Misc Fee',  (fTuition+fMisc) AS 'Cash',fStamp FROM tblPayments t1, " +
                    "(SELECT t22.fOfficialReceipt, fPaymentAmount AS fTuition FROM tblpayments AS t22 WHERE fPaymentFor='TUITION FEE') AS t2, " +
                    " (SELECT t33.fOfficialReceipt, fPaymentAmount AS fMisc FROM tblpayments AS t33 WHERE fPaymentFor='REGISTRATION AND MISC FEE') AS t3 " +
                    "WHERE ((t1.fOfficialReceipt = t2.fOfficialReceipt) OR (t1.fOfficialReceipt = t3.fOfficialReceipt)) AND (fDate = '{0}') GROUP BY t1.fOfficialReceipt", txtSearchDate.Value.ToString("yyyy-MM-dd")));
             */
            us.Script_Add(Script.DBPUX_COMMIT);
            proc.Run(us);
        }
        void CommitPayments()
        {
            UnityScript u = new UnityScript("COMMIT", 7);
            u.Script_Add(Script.DBPUX_BEGIN);
            //Construct Expenses Items
           
                u.Script_Add(Script.DBPUX_INSERT, "tblPayments(fDate, fEnrollmentNumber, fStudentNumber, fName, fOfficialReceipt, fPaymentAmount, fPaymentFor)",
                    new string[]{
                        txtDate.Value.ToString("yyyy-MM-dd"),
                        lstEnrollmentNumber.Text,
                        txtStudentNumber.Text,
                        txtName.Text,
                        txtOR.Text,
                        txtPrice.Text,
                        lstPaymentFor.Text
                    });
            u.Script_Add(Script.DBPUX_COMMIT);
            //Wait if UnityCall is in progress.
            System.Threading.Thread.Sleep(10);
            while (proc.IsRunning)
            {

            }
            proc.Run(u);
        }
          
        void States(bool s)
        {
            grpDetails.Enabled = s;
            grdItems.Enabled = s;
        }

        void ConstructItems()
        {
            DataTable orArr = payment.DefaultView.ToTable(true, "fOfficialReceipt");

            for (int i = 0; i < orArr.Rows.Count; i++)
            {
                DataRow dr = items.NewRow();
                string offRe = orArr.Rows[i]["fOfficialReceipt"].ToString();

                dr[1] = orArr.Rows[i]["fOfficialReceipt"].ToString();
                string sMisc = "", sTuition = "";

                for (int j = 0; j < payment.Rows.Count; j++)
                {
                    
                    if (offRe.Equals(payment.Rows[j]["fOfficialReceipt"].ToString()))
                    {
                        dr[0] = payment.Rows[j]["fName"].ToString();
                        if (payment.Rows[j]["fPaymentFor"].ToString().Equals("TUITION FEE"))
                        {
                            dr[3] = payment.Rows[j]["fPaymentAmount"].ToString();
                            sTuition = payment.Rows[j]["fPaymentAmount"].ToString();
                        }
                        else
                        {
                            dr[4] = payment.Rows[j]["fPaymentAmount"].ToString();
                            sMisc = payment.Rows[j]["fPaymentAmount"].ToString();
                        }
                    }
                }

                if (sMisc.Equals("")) sMisc = "0";
                if (sTuition.Equals("")) sTuition = "0";

                float totalCash = float.Parse(sMisc) + float.Parse(sTuition);
                dr[2] = totalCash.ToString();
                    items.Rows.Add(dr);
            }
        }
        //Printing Module
        int cnt = 0;
        int pp = 1;
        double gTC = 0, gTT = 0, gTM = 0;

        void PrintPO()
        {
            gTC = 0;
            gTT = 0;
            gTM = 0;

            PrintDocument pd = new PrintDocument();
            pd.DocumentName = lblText.Text;
            pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
            PrintDialog pdlg = new PrintDialog();
            pdlg.Document = pd;
            if (pdlg.ShowDialog() == DialogResult.OK)
            {
                PrintPreviewDialog ppdlg = new PrintPreviewDialog();
                ppdlg.Document = pd;
                ppdlg.ShowDialog();
            }
        }
        void pd_PrintPage(object sender, PrintPageEventArgs e)
        {
            float lpp = 0;
            float y = 0;
            float tm = e.MarginBounds.Top;
            float lm = e.PageBounds.Left + 50;
            float lw = e.PageBounds.Width - 50;
            float th = e.PageBounds.Height - 100;
            float tHt = 0;
            Font cFont;
            StringFormat sf = new StringFormat();
            //Title
            cFont = new Font("Arial", 16f, FontStyle.Bold);
            tHt = cFont.GetHeight(e.Graphics);
            y = 50;
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString("GOLDEN WEST COLLEGES", cFont, Brushes.Black, (lw + 100) / 2, y, sf);
            //SubTitle
            y += tHt;
            cFont = new Font("Arial", 8f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            e.Graphics.DrawString("Alaminos, Pangasinan", cFont, Brushes.Black, (lw + 100) / 2, y, sf);
            //SubTitle
            y += tHt;
            cFont = new Font("Arial", 8f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            e.Graphics.DrawString("Date: "+txtSearchDate.Value.ToLongDateString(), cFont, Brushes.Black, (lw + 100) / 2, y, sf);
            
            y += tHt;
            y += tHt;
            y += tHt;
            y += tHt;
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 4, y), new PointF(780, y));

            y += tHt;
            
            //Table Columns
            cFont = new Font("Arial", 11f, FontStyle.Bold);
            tHt = cFont.GetHeight(e.Graphics);
            y += tHt;
            e.Graphics.DrawString("No.", cFont, Brushes.Black, lm + 4, y, new StringFormat());
            e.Graphics.DrawString("Particulars", cFont, Brushes.Black, lm + 85, y, new StringFormat());
            e.Graphics.DrawString("Official Receipt", cFont, Brushes.Black, lm + 225, y, new StringFormat());
            e.Graphics.DrawString("Cash", cFont, Brushes.Black, lm + 390, y, new StringFormat());
            e.Graphics.DrawString("Tuition Fee", cFont, Brushes.Black, lm + 500, y, new StringFormat());
            e.Graphics.DrawString("Reg. and Misc Fee", cFont, Brushes.Black, lm + 612, y, new StringFormat());

            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y - (tHt / 2)), new PointF(lw, y - (tHt / 2)));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y + tHt), new PointF(lw, y + tHt));

            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y - (tHt / 2)), new PointF(lm, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lw, y - (tHt / 2)), new PointF(lw, y + tHt));

            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 35, y - (tHt / 2)), new PointF(lm + 35, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 220, y - (tHt / 2)), new PointF(lm + 220, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 350, y - (tHt / 2)), new PointF(lm + 350, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 480, y - (tHt / 2)), new PointF(lm + 480, y + tHt));
            e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 610, y - (tHt / 2)), new PointF(lm + 610, y + tHt));

            //Table
            y += (tHt * 1.25f);
            cFont = new Font("Arial Narrow", 10f, FontStyle.Regular);
            tHt = cFont.GetHeight(e.Graphics);
            lpp = (th - y) / tHt;
            int cl = 0;
            double tC = 0, tT = 0, tM = 0;
            while (cl <= lpp && cnt < grdItems.Rows.Count)
            {
                sf.Alignment = StringAlignment.Near;
                e.Graphics.DrawString((cnt + 1) + "", cFont, Brushes.Black, lm + 4, y, sf);
                string str = grdItems.Rows[cnt].Cells[0].Value.ToString();
                if(str.Length > 25)
                {
                    str = str.Substring(0, 22) + "...";
                }
                e.Graphics.DrawString("" + str, cFont, Brushes.Black, lm + 36, y, sf);
                e.Graphics.DrawString("" + grdItems.Rows[cnt].Cells[1].Value.ToString(), cFont, Brushes.Black, lm + 222, y, sf);

                sf.Alignment = StringAlignment.Far;
                e.Graphics.DrawString("" + float.Parse(grdItems.Rows[cnt].Cells[2].Value.ToString()).ToString("#,#0.00"), cFont, Brushes.Black, lm + 479, y, sf);
                e.Graphics.DrawString("" + float.Parse(grdItems.Rows[cnt].Cells[3].Value.ToString()).ToString("#,#0.00"), cFont, Brushes.Black, lm + 609, y, sf);
                e.Graphics.DrawString("" + float.Parse(grdItems.Rows[cnt].Cells[4].Value.ToString()).ToString("#,#0.00"), cFont, Brushes.Black, lw + 1, y, sf);

                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y + tHt), new PointF(lw, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm, y - (tHt / 2)), new PointF(lm, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lw, y - (tHt / 2)), new PointF(lw, y + tHt));

                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 35, y - (tHt / 2)), new PointF(lm + 35, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 220, y - (tHt / 2)), new PointF(lm + 220, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 350, y - (tHt / 2)), new PointF(lm + 350, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 480, y - (tHt / 2)), new PointF(lm + 480, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 610, y - (tHt / 2)), new PointF(lm + 610, y + tHt));

                tC += float.Parse(grdItems.Rows[cnt].Cells[2].Value.ToString());
                tT += float.Parse(grdItems.Rows[cnt].Cells[3].Value.ToString());
                tM += float.Parse(grdItems.Rows[cnt].Cells[4].Value.ToString());

                y += tHt;
                cnt++;
                cl++;
            }
            if (cnt < grdItems.Rows.Count)
            {
                y += tHt;
                sf.Alignment = StringAlignment.Far;
                e.Graphics.DrawString(tC.ToString("#,#0.00"), cFont, Brushes.Blue, lm + 479, y, sf);
               
                e.Graphics.DrawString(tT.ToString("#,#0.00"), cFont, Brushes.Blue, lm + 609, y, sf);
                
                e.Graphics.DrawString(tM.ToString("#,#0.00"), cFont, Brushes.Blue, lw + 1, y, sf);

                gTC = gTC + tC;
                gTT = gTT + tT;
                gTM = gTM + tM;

                tC = 0;
                tT = 0;
                tM = 0;

                e.HasMorePages = true;
                pp++;
            }
            else
            {
                gTC = gTC + tC;
                gTT = gTT + tT;
                gTM = gTM + tM;

                y += tHt;
                sf.Alignment = StringAlignment.Far;
                e.Graphics.DrawString(tC.ToString("#,#0.00"), cFont, Brushes.Blue, lm + 479, y, sf);
                e.Graphics.DrawString(gTC.ToString("#,#0.00"), cFont, Brushes.Red, lm + 479, y + tHt, sf);
                
                e.Graphics.DrawString(tT.ToString("#,#0.00"), cFont, Brushes.Blue, lm + 609, y, sf);
                e.Graphics.DrawString(gTT.ToString("#,#0.00"), cFont, Brushes.Red, lm + 609, y + tHt, sf);
               
                e.Graphics.DrawString(tM.ToString("#,#0.00"), cFont, Brushes.Blue, lw + 1, y, sf);
                e.Graphics.DrawString(gTM.ToString("#,#0.00"), cFont, Brushes.Red, lw + 1, y + tHt, sf);
                //Nothing Follows
                y += tHt;
                e.Graphics.DrawString("******************** Nothing Follows ********************", cFont, Brushes.Black, lm + 250, y + tHt, new StringFormat());

                e.HasMorePages = false;
                cnt = 0;
                pp = 1;
            }

            /*
           
            bool nf = false;
            for (int i = 0; i < 30; i++)
            {
                
                if (grdItems.Rows.Count > i)
                {
                    
                   
                    
                    e.Graphics.DrawString("" + grdItems.Rows[i].Cells[4].Value.ToString(), cFont, Brushes.Black, lm + 660, y, new StringFormat());
                }
                else
                {
                    if (!nf)
                    {
                        
                        nf = true;
                    }
                }
                

                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 35, y - (tHt / 2)), new PointF(lm + 35, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 400, y - (tHt / 2)), new PointF(lm + 400, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 560, y - (tHt / 2)), new PointF(lm + 560, y + tHt));
                e.Graphics.DrawLine(new Pen(Color.Black), new PointF(lm + 655, y - (tHt / 2)), new PointF(lm + 655, y + tHt));

                y += (tHt * 1.25f);

            }

            */

        }
    }
    //Code Log:
    //- Speed up entry checking by removing the method call from the timer.
    //- Removed the CheckContent method as the CheckEntry method is sufficient for checking
    //  user inputs.
}